/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dp1.titandevelop.titano.view.prod;

import dp1.titandevelop.titano.bean.Mensajes;
import dp1.titandevelop.titano.persistent.Asignacion;
import dp1.titandevelop.titano.persistent.Demanda;
import dp1.titandevelop.titano.service.AsignacionService;
import dp1.titandevelop.titano.service.OrdenProduccionService;
import java.awt.Cursor;
import java.awt.Dimension;
import java.util.List;
import javax.swing.JOptionPane;


/**
 *
 * @author chrestean
 */
public class ModulosT_PRO_OrdProNuevo extends javax.swing.JInternalFrame {

    public static Demanda dem;
    public static List<Asignacion> asig=null;
    public ModulosT_PRO_DetalleAsignacion ventanaEdita;
    private Mensajes sms = new Mensajes();

    public ModulosT_PRO_OrdProNuevo() {
        initComponents();
        AsignacionService serv = new AsignacionService();
        asig=serv.SacaUltimo();
        txtAsignacion.setText(""+asig.get(0).getCodigoasignacion());
    }
    
    
    
     /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        botonGuardar = new javax.swing.JButton();
        botonSalir = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        btnDetalleAsignacion = new javax.swing.JButton();
        txtAsignacion = new javax.swing.JTextField();
        lblAsignacion = new javax.swing.JLabel();

        setResizable(true);
        setTitle("Orden de Producción");
        setToolTipText("");
        setMaximumSize(new java.awt.Dimension(373, 225));
        setMinimumSize(new java.awt.Dimension(373, 225));
        setPreferredSize(new java.awt.Dimension(370, 180));
        setRequestFocusEnabled(false);

        botonGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/grabar.png"))); // NOI18N
        botonGuardar.setToolTipText("Guardar");
        botonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonGuardarActionPerformed(evt);
            }
        });

        botonSalir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/cancelar.png"))); // NOI18N
        botonSalir.setToolTipText("Salir");
        botonSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonSalirActionPerformed(evt);
            }
        });

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos"));

        btnDetalleAsignacion.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/detalle.png"))); // NOI18N
        btnDetalleAsignacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDetalleAsignacionActionPerformed(evt);
            }
        });

        txtAsignacion.setEnabled(false);
        txtAsignacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtAsignacionActionPerformed(evt);
            }
        });

        lblAsignacion.setText("Asignacion");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblAsignacion)
                .addGap(18, 18, 18)
                .addComponent(txtAsignacion, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnDetalleAsignacion)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnDetalleAsignacion)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblAsignacion)
                        .addComponent(txtAsignacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(10, 10, 10))
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(botonGuardar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botonSalir))
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 20, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 82, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(botonGuardar)
                    .addComponent(botonSalir))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(23, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botonSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonSalirActionPerformed
        ModulosT_PRO_OrdPro ordProd = new ModulosT_PRO_OrdPro();
        this.getParent().add(ordProd);
        
        Dimension desktopSize = this.getParent().getSize();
        Dimension jInternalFrameSize = ordProd.getSize();
        ordProd.setLocation((desktopSize.width - jInternalFrameSize.width) / 2,
                (desktopSize.height - jInternalFrameSize.height) / 4);
        ordProd.show();
        this.dispose();
    }//GEN-LAST:event_botonSalirActionPerformed

    private void botonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonGuardarActionPerformed
        OrdenProduccionService ops = new OrdenProduccionService();
        try
        {
            if(asig!=null){
                if(ops.opActiva_NoIniciada()){// orden de prod activa pero sin relaciones que reflejen q esta utilizandose
                    this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                    ops.generaOP(asig); //no se va a modificar, como la we 
                    this.setCursor(Cursor.getDefaultCursor());
                    JOptionPane.showInternalMessageDialog(this.getRootPane(), sms.getRegistrado(),"Mensaje",JOptionPane.INFORMATION_MESSAGE);
                }
                else{JOptionPane.showInternalMessageDialog(this.getRootPane(),"No se puede generar una orden de produccion!\nYa existe una orden de producción activa e iniciada","Mensaje",JOptionPane.WARNING_MESSAGE);}
            }
            else{
                JOptionPane.showInternalMessageDialog(this.getRootPane(),sms.getCompletarCampos(),"Mensaje",JOptionPane.WARNING_MESSAGE);
            }
        }
        catch (Exception e){
             JOptionPane.showInternalMessageDialog(this.getRootPane(),sms.getError(),"Error",JOptionPane.ERROR_MESSAGE);
        }
        ModulosT_PRO.menuOrden = new ModulosT_PRO_OrdPro();
        this.getParent().add(ModulosT_PRO.menuOrden);
        Dimension desktopSize = this.getParent().getSize();
        Dimension jInternalFrameSize =  ModulosT_PRO.menuOrden.getSize();
        ModulosT_PRO.menuOrden.setLocation((desktopSize.width - jInternalFrameSize.width) / 2,
                (desktopSize.height - jInternalFrameSize.height)/4 );
        ModulosT_PRO.menuOrden.show();
        
        
        
        this.dispose();
             
        
    }//GEN-LAST:event_botonGuardarActionPerformed

    private void txtAsignacionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtAsignacionActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtAsignacionActionPerformed

    private void btnDetalleAsignacionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDetalleAsignacionActionPerformed
            AsignacionService asigs = new AsignacionService();
           
            if (asig!=null) {
                 ventanaEdita = new ModulosT_PRO_DetalleAsignacion(asig,asig.get(0).getCodigoasignacion());
                this.getParent().add(ventanaEdita);     
                Dimension desktopSize = this.getParent().getSize();
                Dimension jInternalFrameSize = ventanaEdita.getSize();
                ventanaEdita.setLocation((desktopSize.width - jInternalFrameSize.width) / 2,(desktopSize.height - jInternalFrameSize.height)/4 );
                ventanaEdita.show();    
            }
            else{
                  JOptionPane.showInternalMessageDialog(this, "Primero debe Ingresar una Asignación", "Aviso", JOptionPane.WARNING_MESSAGE);

        
            }
          
             
    }//GEN-LAST:event_btnDetalleAsignacionActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonGuardar;
    private javax.swing.JButton botonSalir;
    private javax.swing.JButton btnDetalleAsignacion;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JLabel lblAsignacion;
    public static javax.swing.JTextField txtAsignacion;
    // End of variables declaration//GEN-END:variables

    private void setLocationRelativeTo(Object object) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    private Object getparent() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
